package com.mo.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mo.constant.Constants;
import com.mo.mapper.TActivityRemarkmapper;
import com.mo.model.TActivityRemark;
import com.mo.model.TUser;
import com.mo.query.ActivityRemarkQuery;
import com.mo.service.ActivityRemarkService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author mo
 * version
 */
@Service
public class ActivityRemarkServiceImpl implements ActivityRemarkService {

    @Resource
    private TActivityRemarkmapper tActivityRemarkmapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int saveActivityRemark(ActivityRemarkQuery activityRemarkQuery) {
        TActivityRemark tActivityRemark = new TActivityRemark();
        BeanUtils.copyProperties(activityRemarkQuery, tActivityRemark);
        tActivityRemark.setCreateTime(new Date());
        //security的安全上下文获取
        TUser user = (TUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        tActivityRemark.setCreateBy(user.getId());
        return tActivityRemarkmapper.insertSelective(tActivityRemark);
    }

    @Override
    public PageInfo<TActivityRemark> getActivityRemarkByPage(Integer current,ActivityRemarkQuery activityRemarkQuery) {
        //设置PageHelper
        PageHelper.startPage(current, Constants.PAGE_SIZE);
        //查询
        List<TActivityRemark> list = tActivityRemarkmapper.selectActivityRemarkByPage(activityRemarkQuery);
        PageInfo<TActivityRemark> info = new PageInfo<>(list);
        return info;
    }

    @Override
    public TActivityRemark getActivityRemarkById(Integer id) {
        TActivityRemark activityRemark = tActivityRemarkmapper.selectByPrimaryKey(id);
        return activityRemark;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateActivityRemark(ActivityRemarkQuery activityRemarkQuery) {
        TActivityRemark tActivityRemark = new TActivityRemark();
        BeanUtils.copyProperties(activityRemarkQuery, tActivityRemark);
        tActivityRemark.setEditTime(new Date());
        TUser user = (TUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        tActivityRemark.setEditBy(user.getId());
        return tActivityRemarkmapper.updateByPrimaryKeySelective(tActivityRemark);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int delActivityRemarkById(Integer id) {
        //逻辑删除：不删除数据，只是修改一下数据的状态
        //物理删除: 真正的把数据从表里删除
        TActivityRemark activityRemark = new TActivityRemark();
        activityRemark.setId(id);
        activityRemark.setDeleted(1);
        return tActivityRemarkmapper.updateByPrimaryKeySelective(activityRemark);
    }
}
